package com.hht.mainmould;

import android.text.TextUtils;
import android.widget.RadioGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.hht.baselibrary.ARouterUtils;
import com.hht.baselibrary.BaseActivity;
import com.hht.baselibrary.BaseContance;
import com.hht.baselibrary.BaseFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Author: chenhu
 * Time: 2021/4/26 17:27
 * Description do somethings
 */
@Route(path = BaseContance.MAIN_ACTIVITY)
public class MainMouldActivity extends BaseActivity {

    private RadioGroup mainTab;
    private List<Fragment> mFragmentList = new ArrayList<>();
    private BaseFragment mHomeFragment = ARouterUtils.getFragment(BaseContance.FRAGMENT_HOME);
    private BaseFragment mMyFragment = ARouterUtils.getFragment(BaseContance.FRAGMENT_MY);

    @Override
    public int getLayoutResID() {
        return R.layout.main_moudle_layout;
    }

    @Override
    public void initView() {
        mainTab = findViewById(R.id.rg_main);
        changeFragment(MainFragment.class.getName());
        mainTab.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rb_main) {
                    changeFragment(BaseContance.FRAGMENT_HOME);
                } else if (checkedId == R.id.rb_center) {
                    changeFragment(MainFragment.class.getName());
                } else if (checkedId == R.id.rb_user) {
                    changeFragment(BaseContance.FRAGMENT_MY);
                } else {
                    changeFragment(MainFragment.class.getName());
                }

            }
        });
    }


    private void changeFragment(String tag) {
        hideFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(tag);
        if (null != fragment) {
            transaction.show(fragment);
        } else {
            if (TextUtils.equals(tag, BaseContance.FRAGMENT_HOME)) {
                fragment = mHomeFragment;
            } else if (TextUtils.equals(tag, MainFragment.class.getName())) {
                fragment = MainFragment.newInstance();
            } else if (TextUtils.equals(tag, BaseContance.FRAGMENT_MY)) {
                fragment = mMyFragment;
            } else {
                fragment = MainFragment.newInstance();
            }
            if (fragment == null) {
                return;
            }
            mFragmentList.add(fragment);
            transaction.add(R.id.main_layout, fragment, tag);
        }
        transaction.commitAllowingStateLoss();
    }

    /**
     * 隐藏所有Fragment
     */
    private void hideFragment() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        for (Fragment f : mFragmentList) {
            transaction.hide(f);
        }
        transaction.commit();

    }
}
